#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>

#include "vpu_test.h"

#include "soc_check.h"

#include "vpu.h"
#include "enc.h"


sigset_t sigset;
int quitflag;
int vpu_test_dbg_level;

struct cmd_line cmd;

static int
signal_thread(void *arg)
{
    int sig;

    pthread_sigmask(SIG_BLOCK, &sigset, NULL);

    while (1) {
        sigwait(&sigset, &sig);
        if (sig == SIGINT) {
            warn_msg("Ctrl-C received\n");
        } else {
            warn_msg("Unknown signal. Still exiting\n");
        }
        quitflag = 1;
        break;
    }

    return 0;
}

#define H264
int EncodeFun(char *time)
{
    printf("Start Encode\n");

    char fileName[100];
    memset(fileName,0,100) ;

    strcat(fileName,time);
    strcat(fileName,".264");

    LockVedioRecord();


    int mode = 1;
#ifndef H264

    cmd.format = 0;
    cmd.width  = 720;
    cmd.height = 576;
    strncpy(cmd.output, "vpu_demo.m4v", MAX_PATH);
    cmd.dst_scheme = PATH_FILE;
    cmd.chromaInterleave = 1;
    cmd.fps = 25;
#else
    cmd.format = STD_AVC;
    cmd.width = 720;
    cmd.height = 576;
//    strncpy(cmd.input, "test.yuv", MAX_PATH);
    strncpy(cmd.output, "test.264", MAX_PATH);

//    strncpy(cmd.output, fileName, MAX_PATH);


    cmd.chromaInterleave = 1;
    cmd.dst_scheme = PATH_FILE;
//    cmd.src_scheme = PATH_FILE;
    cmd.src_scheme = PATH_V4L2;
//    cmd.count = 50;

    cmd.mapType = LINEAR_FRAME_MAP;

#endif

    int err,  ret;
    char  *dbg_env;
    pthread_t sigtid;
#ifdef COMMON_INIT
    vpu_versioninfo ver;
#endif


    char *soc_list[] = {"i.MX6Q", "i.MX6QP", "i.MX6DL", " "};

    ret = soc_version_check(soc_list);
    if (ret == 0)
    {
        printf("mxc_vpu_test.out not supported on current soc\n");
        return 0;
    }

    ret = 0;

    sigemptyset(&sigset);
    sigaddset(&sigset, SIGINT);
    pthread_sigmask(SIG_BLOCK, &sigset, NULL);
    pthread_create(&sigtid, NULL, (void *)&signal_thread, NULL);

#ifndef COMMON_INIT
    srand((unsigned)time(0)); /* init seed of rand() */
#endif

    dbg_env=getenv("VPU_TEST_DBG");
    if (dbg_env)
        vpu_test_dbg_level = atoi(dbg_env);
    else
        vpu_test_dbg_level = 0;


    info_msg("VPU test program built on %s %s\n", __DATE__, __TIME__);

    err = vpu_Init(NULL);
    if (err)
    {
        err_msg("VPU Init Failure.\n");
        return -1;
    }

    err = vpu_GetVersionInfo(&ver);
    if (err)
    {
        err_msg("Cannot get version info, err:%d\n", err);
        vpu_UnInit();
        return -1;
    }

    info_msg("VPU firmware version: %d.%d.%d_r%d\n", ver.fw_major, ver.fw_minor,ver.fw_release, ver.fw_code);
    info_msg("VPU library version: %d.%d.%d\n", ver.lib_major, ver.lib_minor, ver.lib_release);

    if(check_params(&cmd, mode) == 0)
    {
        if(open_files(&cmd) == 0)
        {
            ret = encode_test(&cmd);
        }
    }


#ifdef COMMON_INIT
    vpu_UnInit();
#endif
    printf("Stop Record\n");
    UnLockVedioRecord();

    return ret;
}


